package com.link.base.base.order.service;

import com.link.base.base.order.model.ApiOrder;
import com.link.base.base.order.model.SaleOrder;
import com.link.base.base.order.model.SaleOrderItem;
import com.link.base.core.apiutils.ApiBasicController;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 戴晨雨
 * @date 2019/1/29
 * @comments api订单相关
 */
@Service
public class ApiSaleOrderServiceImpl implements ApiSaleOrderService {
    @Resource
    private KeyGenerateService keyGenerateService;
    @Resource
    private SaleOrderService saleOrderService;
    @Resource
    private SaleOrderItemService saleOrderItemService;

    @Override
    public Long apiOrderUpsert(ApiOrder model) throws Exception {
        SaleOrder saleOrder = check(model);
        List<SaleOrderItem> apiOrderItemList = model.getOrderItemList();
        if (saleOrder == null) {
            saleOrder = new SaleOrder();
            Long id = keyGenerateService.keyGenerate();
            ApiBasicController.apiCopyProperties(model, saleOrder);
            saleOrder.setId(id);
            saleOrder.setSource("SIEBEL");
            saleOrderService.insert(saleOrder);
            if (apiOrderItemList != null && apiOrderItemList.size() > 0) {
                setOrderItems(id, apiOrderItemList, true);
            }
        } else {
            ApiBasicController.apiCopyProperties(model, saleOrder);
            saleOrder.setAnonymousFlag("Y");
            saleOrderService.update(saleOrder);
            if (apiOrderItemList != null && apiOrderItemList.size() > 0) {
                setOrderItems(saleOrder.getId(), apiOrderItemList, false);
            }
        }

        return saleOrder.getId();
    }

    /**
     * <p>检查该记录是否已存在，是则返回对象，否则返回null</p>
     * @author wx
     * @param model
     * @return
     * @throws Exception
     */
    private SaleOrder check(SaleOrder model) throws Exception {
        Long id = model.getCrmId();
        String integrationId = model.getIntegrationId();
        if (StringUtils.isBlank(integrationId)) {
            throw new ServiceException("ORDER-001");
        }
        SaleOrder saleOrder = null;
        //通过集成ID校验记录
        if ( null != id) {
            SaleOrder example = new SaleOrder();
            example.setIntegrationId(integrationId);
            example.setPageFlag(false);
            List<SaleOrder> list = saleOrderService.queryByExamplePage(example);
            if (list != null && list.size() > 0) {
                if (list.size() == 1) {
                    saleOrder = list.get(0);
                } else {
                    throw new ServiceException("ORDER-002");
                }
            }
        } else {
            //通过linkcrm订单ID校验记录
            SaleOrder example = new SaleOrder();
            example.setId(id);
            saleOrder = saleOrderService.queryById(example);
            if (saleOrder == null) {
                throw new ServiceException("ORDER-003");
            } else if (!integrationId.equals(saleOrder.getIntegrationId())) {
                throw new ServiceException("ORDER-004");
            }
        }
        return saleOrder;
    }

    ;

    /**
     * <p>写入订单的订单行信息</p>
     * @author wx
     * @param orderId linkcrm订单ID
     * @param list    linkcrm的订单行list
     * @param isNew   是否新建订单
     * @throws Exception
     *
     */
    private void setOrderItems(Long orderId, List<SaleOrderItem> list, boolean isNew) throws Exception {
        if (isNew) {
            for (SaleOrderItem apiOrderItem : list) {
                SaleOrderItem entity = new SaleOrderItem();
                ApiBasicController.apiCopyProperties(apiOrderItem, entity);
                Long id = keyGenerateService.keyGenerate();
                entity.setId(id);
                entity.setHeadId(orderId);
                entity.setIntegrationId(apiOrderItem.getIntegrationId());
                saleOrderItemService.insert(entity);
            }
        } else {
            for (SaleOrderItem apiOrderItem : list) {
                SaleOrderItem saleOrderItem = getOrderItem(apiOrderItem);
                if (saleOrderItem == null) {
                    SaleOrderItem entity = new SaleOrderItem();
                    ApiBasicController.apiCopyProperties(apiOrderItem, entity);
                    Long id = keyGenerateService.keyGenerate();
                    entity.setId(id);
                    entity.setHeadId(orderId);
                    entity.setIntegrationId(apiOrderItem.getIntegrationId());
                    saleOrderItemService.insert(entity);
                } else {
                    //SaleOrderItem entity = new SaleOrderItem();
                    ApiBasicController.apiCopyProperties(apiOrderItem, saleOrderItem);
                    saleOrderItemService.update(saleOrderItem);
                }
            }
        }
    }

    /**
     * <p>根据api订单行获取linkcrm订单行</p>
     * @author wx
     * @param apiOrderItem
     * @return
     * @throws Exception
     *
     */
    private SaleOrderItem getOrderItem(SaleOrderItem apiOrderItem) throws Exception {
        SaleOrderItem saleOrderItem = null;
        String integrationId = apiOrderItem.getIntegrationId();
        if (StringUtils.isBlank(integrationId)) {
            throw new ServiceException("ORDER-005");
        }

        SaleOrderItem example = new SaleOrderItem();
        example.setIntegrationId(integrationId);
        example.setPageFlag(false);

        List<SaleOrderItem> list = saleOrderItemService.queryByExamplePage(example);
        if (list != null && list.size() > 0) {
            if (list.size() == 1) {
                saleOrderItem = list.get(0);
            } else {
                throw new ServiceException("ORDER-006",integrationId);
            }
        }
        return saleOrderItem;
    }

}
